package com.ling.service;

import com.ling.bean.UserFollow;
import com.ling.bean.UserFollowExample;
import com.ling.dao.UserFollowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserFollowService {
    private final UserFollowMapper userFollowMapper;

    @Autowired
    public UserFollowService(UserFollowMapper userFollowMapper) {
        this.userFollowMapper = userFollowMapper;
    }

    //查询是否已关注
    public boolean hasFollowed(UserFollow userFollow) {
        UserFollowExample userFollowExample = new UserFollowExample();
        userFollowExample.createCriteria().andUseridEqualTo(userFollow.getUserid()).andFollowinguseridEqualTo(userFollow.getFollowinguserid());
        List<UserFollow> userFollows = userFollowMapper.selectByExample(userFollowExample);
        return !userFollows.isEmpty();
    }

    //查询指定用户userId关注的所有博主，按照积分逆序
    public List<UserFollow> getUserFollows(String userId) {
        return userFollowMapper.selectUserFollowByUserId(userId);
    }


    //增加
    public void saveUserFollow(UserFollow userFollow) {
        userFollowMapper.insertSelective(userFollow);
    }

    //删除
    public void deleteUserFollow(UserFollow userFollow) {
        UserFollowExample userFollowExample = new UserFollowExample();
        userFollowExample.createCriteria().andUseridEqualTo(userFollow.getUserid()).andFollowinguseridEqualTo(userFollow.getFollowinguserid());
        userFollowMapper.deleteByExample(userFollowExample);
    }
}
